import java.util.Arrays;
import java.util.Random;
public class RandomTest {
    public static void main(String[] args) {
        int [] arr = new int[5];//准备一个长度为5的数组
        int index = 0;
        for (int i = 0; i < arr.length; i++) {//赋初值
            arr[i] = -1;
        }
        Random random = new Random();//随机数对象
        while (index < arr.length) {
            int tmp = random.nextInt(5);//产生随机数
            if(search(arr,tmp)) {
                arr[index++] = tmp;
            }
        }
        System.out.println(Arrays.toString(arr));

    }

    /**
     *
     * @param arr
     * @param key
     * @return
     */
    public static boolean search (int [] arr, int key) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == key) {
                return  false;//找到了
            }
        }
        return true;//没找到


    }

}
